package com.ruoyi.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.DictConstants;
import lombok.RequiredArgsConstructor;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.web.page.Condition;
import com.ruoyi.tws.api.domain.Payment;
import com.ruoyi.tws.service.IPaymentService;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 支付Controller
 * 
 * @author ruoyi
 * @date 2023-02-23
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/payment")
public class PaymentController extends BaseController {

    private final IPaymentService iPaymentService;

    /**
     * 查询支付列表
     */
    @PreAuthorize(hasPermi="tws:payment:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, Payment payment) {
        return getTD(query, payment);
    }

    /**
     * 查询待支付列表
     */
    @PreAuthorize(hasPermi="tws:payment:list")
    @GetMapping("/payList")
    public TableDataInfo payList(Query query, Payment payment) {
        payment.setStatus(DictConstants.PAYMENT_STATUS.YSQ.getKey());
        return getTD(query, payment);
    }

    private TableDataInfo getTD(Query query, Payment payment) {
        LambdaQueryWrapper<Payment> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(payment.getDocumentNumber())) {
            lqw.eq(Payment::getDocumentNumber, payment.getDocumentNumber());
        }
        if (StringUtils.isNotBlank(payment.getCompanyName())) {
            lqw.like(Payment::getCompanyName, payment.getCompanyName());
        }
        if (StringUtils.isNotBlank(payment.getDriverName())) {
            lqw.like(Payment::getDriverName ,payment.getDriverName());
        }
        if (StringUtils.isNotBlank(payment.getApplyUserName())) {
            lqw.like(Payment::getApplyUserName ,payment.getApplyUserName());
        }
        if (StringUtils.isNotBlank(payment.getPayUserName())) {
            lqw.like(Payment::getPayUserName ,payment.getPayUserName());
        }
        if (StringUtils.isNotBlank(payment.getPayType())) {
            lqw.eq(Payment::getPayType, payment.getPayType());
        }
        if (StringUtils.isNotBlank(payment.getPaymentMeansCode())) {
            lqw.eq(Payment::getPaymentMeansCode, payment.getPaymentMeansCode());
        }
        if (StringUtils.isNotBlank(payment.getRecipient())) {
            lqw.eq(Payment::getRecipient, payment.getRecipient());
        }
        if (StringUtils.isNotBlank(payment.getStatus())) {
            lqw.eq(Payment::getStatus, payment.getStatus());
        }
        if (StringUtils.isNotBlank(payment.getIsSendToPro())) {
            lqw.eq(Payment::getIsSendToPro, payment.getIsSendToPro());
        }
        lqw.orderByDesc(Payment::getCreateTime);
        IPage<Payment> list = iPaymentService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 导出支付列表
     */
    @PreAuthorize(hasPermi = "tws:payment:export")
    @Log(title = "支付", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Payment payment) throws IOException
    {
        LambdaQueryWrapper<Payment> lqw = new LambdaQueryWrapper<>(payment);
        List<Payment> list = iPaymentService.list(lqw);
        ExcelUtil<Payment> util = new ExcelUtil<Payment>(Payment. class);
        util.exportExcel(response, list, "payment");
    }

    /**
     * 获取支付详细信息
     */
    @PreAuthorize(hasPermi="tws:payment:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iPaymentService.getById(id));
    }

    /**
     * 新增支付
     */
    @PreAuthorize(hasPermi="tws:payment:add")
    @Log(title = "支付", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Payment payment) {
        return toAjax(iPaymentService.add(payment) ? 1 : 0);
    }

    /**
     * 线下支付
     */
    @PreAuthorize(hasPermi="tws:payment:edit")
    @Log(title = "支付", businessType = BusinessType.UPDATE)
    @PostMapping("/payoff")
    public AjaxResult payoff(@RequestBody Payment payment) {
        return toAjax(iPaymentService.payoff(payment) ? 1 : 0);
    }

    /**
     * 修改支付
     */
    @PreAuthorize(hasPermi="tws:payment:edit")
    @Log(title = "支付" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Payment payment) {
        return toAjax(iPaymentService.updateById(payment) ? 1 : 0);
    }

    /**
     * 删除支付
     */
    @PreAuthorize(hasPermi="tws:payment:remove")
    @Log(title = "支付", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iPaymentService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 基于资金单上传订单、司机、车辆至省平台
     */
    @GetMapping(value = "/upload/{id}")
    public AjaxResult upload(@PathVariable("id") Long id) {
        return toAjax(iPaymentService.upload(id) ? 1 : 0);
    }

}
